cc.Class({
    extends: cc.Component,

    properties: {
        pfTip: cc.Prefab,
    },

    onLoad() {
        this.canvas = this.node.getComponent(cc.Canvas);

        window.showGameToast = (str) => {

            let canvas = cc.find("Canvas");
            if (canvas) {
                let ndTip = cc.instantiate(this.pfTip);
                ndTip.parent = canvas;
                ndTip.getComponent("pfTip").setTip(str);
            }
        }
    },

    onClickRotate() {
        this._dir = this._dir == 'V' ? 'H' : 'V'  //H横屏  V竖屏
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'setOrientation', '(Ljava/lang/String;)V', this._dir)
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'setOrientation:', this._dir)
        }

        let frameSize = cc.view.getFrameSize()
        console.log('frameSize: ' + frameSize.width + '   ' + frameSize.height)
        if (this._dir == 'V') {
            cc.view.setOrientation(cc.macro.ORIENTATION_PORTRAIT);
            if (frameSize.width > frameSize.height) {
                cc.view.setFrameSize(frameSize.height, frameSize.width)
            }

            this.canvas.designResolution = cc.size(720, 1280)

            // this.canvas.fitHeight = false;
            // this.canvas.fitWidth = true;
            // cc.view.setDesignResolutionSize(720, 1280, cc.ResolutionPolicy.FIXED_WIDTH);
        }
        else {
            cc.view.setOrientation(cc.macro.ORIENTATION_LANDSCAPE);
            if (frameSize.height > frameSize.width) {
                cc.view.setFrameSize(frameSize.height, frameSize.width)
            }

            this.canvas.designResolution = cc.size(1280, 720)

            // this.canvas.fitHeight = true;
            // this.canvas.fitWidth = false;
            // cc.view.setDesignResolutionSize(1280, 720, cc.ResolutionPolicy.FIXED_HEIGHT);
        }

        //手动调用触发 Wdiget 组件重新布局
        if (window.jsb) {
            window.dispatchEvent(new cc.Event.EventCustom('resize', true))
        }
    },

    onClickShowToast() {
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'showToast', '(Ljava/lang/String;)V', "我是一条log")
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'showToast:', "我也是一条log");
        }
    },

    onClickCopy() {
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'copyStr', '(Ljava/lang/String;)V', "我是一条log")
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'copyStr:', "我也是一条log");
        }
    },

    onClickVersionName() {
        let versionName = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 String类型：Ljava/lang/String;，后面有个;号，不能缺
            versionName = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAppVersionName', '()Ljava/lang/String;');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            versionName = jsb.reflection.callStaticMethod('AppController', 'getAppVersionName');
        }

        console.log("versionName:", versionName);
    },

    onClickVersionCode() {
        let versionCode = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            //注意 返回类型为void 要写明 V  但是 无参数，不必写明 ()就行
            versionCode = jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'getAppVersionCode', '()I');
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            versionCode = jsb.reflection.callStaticMethod('AppController', 'getAppVersionCode');
        }
        console.log("versionCode：", versionCode);
    },

    onClickShowLog() {

        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod('org/cocos2dx/javascript/AppActivity', 'showLog', '(Ljava/lang/String;)V', "我是一个log测试");
        } else if (cc.sys.os == cc.sys.OS_IOS) {
            jsb.reflection.callStaticMethod('AppController', 'showLog:', "我是一个log测试");
        }

    },

});
